package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.pojo.Dish;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * Rest模式
 * */
@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    //分页查询
    @GetMapping("page")
    public R<Page> page(Integer page, Integer pageSize) {
        //创建分页构造器
        Page<Category> categoryIPage = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        queryWrapper.orderByAsc(Category::getSort);
        //执行分页查询
        categoryService.page(categoryIPage, queryWrapper);

        return R.success(categoryIPage);
    }

    //新增菜品及套餐分类
    @PostMapping
    public R<String> save(@RequestBody Category category) {
        try {
            categoryService.save(category);
            return R.success("新增成功!");
        } catch (Exception e) {
            log.error("新增失败!");
            throw new RuntimeException(e);
        }
    }

    //修改菜品及套餐分类
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        try {
            categoryService.updateById(category);
            return R.success("修改成功!");
        } catch (Exception e) {
            log.info("修改失败!");
            throw new RuntimeException(e);
        }
    }

    //删除菜品及套餐分类
    @DeleteMapping
    public R<String> delete(@RequestParam("ids") String id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        List<Dish> dishList = dishService.list(dishLambdaQueryWrapper);

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        List<Setmeal> setmealList = setmealService.list(setmealLambdaQueryWrapper);
        if (dishList.size() != 0) {
            return R.error("删除失败！原因:该分类关联了菜品");
        } else if (setmealList.size() != 0) {
            return R.error("删除失败！原因:该分类关联了套餐");
        }
        try {
            categoryService.removeById(id);
            return R.success("删除成功!");
        } catch (Exception e) {
            log.info("删除失败");
            throw new RuntimeException(e);
        }
    }

    /**
     * 查询所有菜品
     *
     * @param type 类型:1.菜品分类 2.套餐分类
     * @return
     */
    @GetMapping("list")
    public R<List> list(Integer type) {
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Category::getType, type);
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        return R.success(categoryService.list(lambdaQueryWrapper));
    }
}
